package grpc

import (
	"context"
	"google.golang.org/grpc"
	"webook/api/proto/gen/search/v1"
	"webook/search/domain"
	"webook/search/service"
)

type SyncServiceServer struct {
	searchv1.UnimplementedSyncServiceServer
	svc service.SyncService
}

func NewSyncServiceServer(svc service.SyncService) *SyncServiceServer {
	return &SyncServiceServer{
		svc: svc,
	}
}

func (s *SyncServiceServer) Register(server grpc.ServiceRegistrar) {
	searchv1.RegisterSyncServiceServer(server, s)
}

func (s *SyncServiceServer) InputUser(ctx context.Context, request *searchv1.InputUserRequest) (*searchv1.InputUserResponse, error) {
	user := request.GetUser()
	err := s.svc.InputUser(ctx, domain.User{
		Id:       user.GetId(),
		Email:    user.GetEmail(),
		Nickname: user.GetNickname(),
		Phone:    user.GetPhone(),
	})
	return &searchv1.InputUserResponse{}, err
}

func (s *SyncServiceServer) InputArticle(ctx context.Context, request *searchv1.InputArticleRequest) (*searchv1.InputArticleResponse, error) {
	article := request.GetArticle()
	err := s.svc.InputArticle(ctx, domain.Article{
		Id:      article.GetId(),
		Title:   article.GetTitle(),
		Content: article.GetContent(),
		Status:  article.GetStatus(),
		Tags:    article.GetTags(),
	})
	return &searchv1.InputArticleResponse{}, err
}

func (s *SyncServiceServer) InputAny(ctx context.Context, request *searchv1.InputAnyRequest) (*searchv1.InputAnyResponse, error) {
	err := s.svc.InputAny(ctx, request.GetIndexName(), request.GetDocId(), request.GetData())
	return &searchv1.InputAnyResponse{}, err
}
